package com.cxl.service.impl;

import com.cxl.entity.Payment;
import com.cxl.esrepository.EsRepository;
import com.cxl.repository.PaymentRepository;
import com.cxl.service.PaymenService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang.StringUtils;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.RangeQueryBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.SearchHit;
import org.springframework.data.elasticsearch.core.SearchHits;
import org.springframework.data.elasticsearch.core.query.NativeSearchQuery;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: 20971
 * @time: 2022/3/5 13:42
 * @description:
 */
@Service
public class PaymenServiceImpl implements PaymenService {

    @Autowired
    private EsRepository esRepository;

    @Autowired
    private PaymentRepository paymentRepository;

    @Autowired
    private ElasticsearchRestTemplate  elasticsearchRestTemplate;

    @Override
    public PageInfo<Payment> findByList(Payment payment, Integer pageNum, Integer pageSize) {
        esRepository.deleteAll();

        if (esRepository.count() == 0) {
            List<Payment> list = paymentRepository.findAll();
            esRepository.saveAll(list);
        }

        //组合多条件查询
        BoolQueryBuilder queryBuilder = new BoolQueryBuilder();

        /*付款状态*/
        if (payment.getState() != null) {
            queryBuilder.must(QueryBuilders.termQuery("state", payment.getState()));
        }
        /*费用类型*/
        if (payment.getCostType()!=null) {
            if (payment.getCostType().getCid()!=null) {
                queryBuilder.must(QueryBuilders.termQuery("costType.cid", payment.getCostType().getCid()));
            }
        }
        /*房产信息*/
        if (StringUtils.isNotBlank(payment.getOwnerName())) {
            queryBuilder.must(QueryBuilders.matchQuery("ownerName", payment.getOwnerName()));

        }
        RangeQueryBuilder paymentDate = QueryBuilders.rangeQuery("indate");
        /*付款日期*/
        if (payment.getStateDate() != null) {
            paymentDate.gte(payment.getStateDate().getTime());
        }
        if (payment.getEndDate() != null) {
            paymentDate.lte(payment.getEndDate().getTime());
        }
        queryBuilder.must(paymentDate);
        //高亮
        HighlightBuilder highlightBuilder = new HighlightBuilder();
        highlightBuilder.preTags("<font color='red'>");
        highlightBuilder.postTags("</font>");

        //封装查询
        NativeSearchQuery searchQuery = new NativeSearchQueryBuilder()
                .withPageable(PageRequest.of(pageNum - 1, pageSize))
                .withQuery(queryBuilder)
                .withHighlightBuilder(highlightBuilder)
                .build();
        //查询结果
        SearchHits<Payment> search = elasticsearchRestTemplate.search(searchQuery, Payment.class);
        ArrayList<Payment> payments = new ArrayList<>();
        for (SearchHit<Payment> paymentSearchHit : search) {
            Payment content = paymentSearchHit.getContent();
            System.out.println(paymentSearchHit.getHighlightField("ownerName"));
            if (paymentSearchHit.getHighlightField("ownerName") != null && paymentSearchHit.getHighlightField("ownerName").size() > 0) {
                String ownerName = paymentSearchHit.getHighlightField("ownerName").get(0);
                content.setOwnerName(ownerName);
            }
            payments.add(content);
        }

        Page<Payment> page = new Page<>(pageNum, pageSize);
        page.setTotal(search.getTotalHits());
        PageInfo<Payment> pageInfo = new PageInfo<>(page);
        pageInfo.setList(payments);

        return pageInfo;
    }
}
